using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JGSY.CMS.LowCode.Platform.Domain.Entities.Base;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 租户实体
    /// 多租户系统的核心实体，管理租户基本信息和配置
    /// </summary>
    [Table("tenants")]
    public class Tenant : AuditableEntity
    {
        /// <summary>
        /// 租户唯一标识符
        /// 用于API调用和数据隔离的全局唯一ID
        /// </summary>
        [Required]
        [StringLength(36)] // UUID格式
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 租户名称
        /// </summary>
        [Required]
        [StringLength(100)]
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 租户显示名称
        /// </summary>
        [StringLength(200)]
        public string? DisplayName { get; set; }

        /// <summary>
        /// 租户描述
        /// </summary>
        [StringLength(1000)]
        public string? Description { get; set; }

        /// <summary>
        /// 租户子域名
        /// </summary>
        [StringLength(50)]
        public string? SubDomain { get; set; }

        /// <summary>
        /// 自定义域名
        /// </summary>
        [StringLength(100)]
        public string? CustomDomain { get; set; }

        /// <summary>
        /// 租户状态
        /// Active=1, Suspended=2, Inactive=3
        /// </summary>
        public TenantStatus Status { get; set; } = TenantStatus.Active;

        /// <summary>
        /// 订阅计划
        /// </summary>
        [StringLength(50)]
        public string? SubscriptionPlan { get; set; }

        /// <summary>
        /// 租户配置信息 (JSON格式)
        /// </summary>
        [Column(TypeName = "json")]
        public string? Configuration { get; set; }

        /// <summary>
        /// 资源限制配置 (JSON格式)
        /// </summary>
        [Column(TypeName = "json")]
        public string? ResourceLimits { get; set; }

        /// <summary>
        /// 到期时间
        /// </summary>
        public DateTime? ExpiresAt { get; set; }

        /// <summary>
        /// 最后活动时间
        /// </summary>
        public DateTime? LastActivityAt { get; set; }

        /// <summary>
        /// 数据库连接字符串（可选，支持独立数据库）
        /// </summary>
        [StringLength(1000)]
        public string? DatabaseConnectionString { get; set; }

        /// <summary>
        /// 租户联系邮箱
        /// </summary>
        [StringLength(100)]
        public string? ContactEmail { get; set; }

        /// <summary>
        /// 租户联系电话
        /// </summary>
        [StringLength(20)]
        public string? ContactPhone { get; set; }

        /// <summary>
        /// 租户时区
        /// </summary>
        [StringLength(50)]
        public string TimeZone { get; set; } = "Asia/Shanghai";

        /// <summary>
        /// 租户语言
        /// </summary>
        [StringLength(10)]
        public string Language { get; set; } = "zh-CN";

        /// <summary>
        /// Logo URL
        /// </summary>
        [StringLength(500)]
        public string? LogoUrl { get; set; }

        /// <summary>
        /// 主题配置 (JSON格式)
        /// </summary>
        [Column(TypeName = "json")]
        public string? ThemeConfiguration { get; set; }

        // 导航属性

        /// <summary>
        /// 租户用户集合
        /// </summary>
        [InverseProperty(nameof(User.Tenant))]
        public ICollection<User> Users { get; set; } = new List<User>();

        /// <summary>
        /// 租户角色集合
        /// </summary>
        [InverseProperty(nameof(Role.Tenant))]
        public ICollection<Role> Roles { get; set; } = new List<Role>();
    }

    /// <summary>
    /// 租户状态枚举
    /// </summary>
    public enum TenantStatus
    {
        /// <summary>
        /// 活跃状态
        /// </summary>
        Active = 1,

        /// <summary>
        /// 暂停状态
        /// </summary>
        Suspended = 2,

        /// <summary>
        /// 非活跃状态
        /// </summary>
        Inactive = 3
    }
}
